package main

import (
	"fmt"
	"net"
)

func main() {
	// 服务端
	listen, err := net.Listen("tcp", "127.0.0.1:8080")
	if err != nil {
		fmt.Println(err)
	}
	for {

		fmt.Println("server is waiting!")
		conn, err := listen.Accept()
		if err != nil {
			fmt.Println(err)
		}

		// 接收来自客户端浏览器的请求信息
		data := make([]byte, 1024)
		conn.Read(data)
		n, _ := conn.Read(data)

		// data一定要符合http请求协议格式
		fmt.Print("data:\n", string(data[:n]))
		res := "HTTP/1.1 200 ok\r\ncontent-type:text/plain\r\n\r\nhello world"
		conn.Write([]byte(res))
		conn.Close()
	}
}
